package com.dzl.fee.service.impl;

import com.dzl.card.service.WelfareCardSecretService;
import com.dzl.config.exception.CommonException;
import com.dzl.constant.CommonConstants;
import com.dzl.exchange.service.WelfareExchangeSecretService;
import com.dzl.fee.pojo.WelfareFeeUserCustomers;
import com.dzl.fee.pojo.WelfareFeeUserSales;
import com.dzl.fee.pojo.query.WelfareSalesCustomersQuery;
import com.dzl.fee.service.WelfareFeeUserCustomersService;
import com.dzl.fee.service.WelfareFeeUserSalesService;
import com.dzl.fee.service.WelfareSalesCustomersService;
import com.dzl.util.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务员客户关联关系服务接口实现类
 * @date 2021/9/18 9:54
 * @author caobucao
 */
@Service
public class WelfareSalesCustomersServiceImpl implements WelfareSalesCustomersService {

    @Autowired
    private WelfareCardSecretService cardSecretService;
    @Autowired
    private WelfareExchangeSecretService exchangeSecretService;
    @Autowired
    private WelfareFeeUserCustomersService welfareFeeUserCustomersService;
    @Autowired
    private WelfareFeeUserSalesService welfareFeeUserSalesService;

    @Override
    public Map<String, Object> querySalesAndCustomers(WelfareSalesCustomersQuery query) {
        Map<String, Object> map = new HashMap<>();
        if(EmptyUtils.isNotEmpty(query.getCardId()) && EmptyUtils.isEmpty(query.getExchangeId())) {
            //查卡业务员
            List<WelfareFeeUserSales> salesList = cardSecretService.selectSalesByCardId(query);
            //查卡客户
            List<WelfareFeeUserCustomers> customersList = cardSecretService.selectCustomersByCardId(query);
            map.put("salesList", salesList);
            map.put("customersList", customersList);
        } else if (EmptyUtils.isEmpty(query.getCardId()) && EmptyUtils.isNotEmpty(query.getExchangeId())) {
            //查该券密组的业务员
            List<WelfareFeeUserSales> salesList = exchangeSecretService.selectSalesByExchangeId(query);
            //查该券密组的客户
            List<WelfareFeeUserCustomers> customersList = exchangeSecretService.selectCustomersByExchangeId(query);
            map.put("salesList", salesList);
            map.put("customersList", customersList);
        } else if ((EmptyUtils.isNotEmpty(query.getCardId()) && EmptyUtils.isNotEmpty(query.getExchangeId()))) {
            throw new CommonException("卡id与券密组id必传且只能传一项！");
        } else if (EmptyUtils.isEmpty(query.getCardId()) && EmptyUtils.isEmpty(query.getExchangeId())) {
            WelfareFeeUserSales welfareFeeUserSalesQuery = new WelfareFeeUserSales();
            welfareFeeUserSalesQuery.setStatus(CommonConstants.YES);
            welfareFeeUserSalesQuery.setEnterpriseId(query.getEnterpriseId());
            List<WelfareFeeUserSales> salesList = welfareFeeUserSalesService.select(welfareFeeUserSalesQuery);
            //查卡客户
            WelfareFeeUserCustomers welfareFeeUserCustomersQuery = new WelfareFeeUserCustomers();
            welfareFeeUserCustomersQuery.setStatus(CommonConstants.YES);
            welfareFeeUserCustomersQuery.setEnterpriseId(query.getEnterpriseId());
            List<WelfareFeeUserCustomers> customersList = welfareFeeUserCustomersService.select(welfareFeeUserCustomersQuery);
            map.put("salesList", salesList);
            map.put("customersList", customersList);
        }
        return map;
    }
}
